<?php

//use Illuminate\Support\Facades\Redis;
class Api_Controller extends Base_Controller {

	public function action_index()
	{
		
		if($this->checkSignature()){
			$echostr = $_GET['echostr'];
			echo $echostr;
			exit;
		}


	}
    

    //验证微信是否绑定了url
    private function checkSignature()  
    {  
      $signature = $_GET["signature"];  
      $timestamp = $_GET["timestamp"];  
      $nonce = $_GET["nonce"];              
      $token = 'weixin';  
      $tmpArr = array($nonce, $timestamp, $token);  
      sort($tmpArr);  
      $tmpStr = implode( $tmpArr );  
      $tmpStr = sha1( $tmpStr );  
        
      if( $tmpStr == $signature ){  
          return true;  
      }else{  
          return false;  
      }  
    }


    /*
    判断是否中奖的接口;返回json数据 
    {
      "code":200,
      "errmsg":"恭喜中奖",
    }
    {
      "code":400,
      "errmsg":"很遗憾",
    }
    */
    public function action_isprize()
    {

      header("Access-Control-Allow-Origin: *");
      header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With'); 
      $popula = DB::table('population')->first();
      $count = DB::table('user_member')->count();
      $population = $popula['popula'];  //0.009
      if($population!=0&&$population!=1){
        $intnumber = explode(".",$population)[1];  //009
        $mun =  strlen($intnumber); //获取小数点后的位数 3
        $munber = rand(0,pow(10,$mun)); // (0-1000)
        if($munber<=$intnumber){
          return json_encode(array('msg'=>'yse','code'=>200));
        }else{
          return json_encode(array('msg'=>'sorry','code'=>400));
        }
      }else{
        return json_encode(array('msg'=>'sorry','code'=>400));
      }
      if($count==40){
        return json_encode(array('msg'=>'sorry','code'=>400));
      }
    }


    public function action_getuserinfo()
    {

      header("Access-Control-Allow-Origin: *");
      header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With'); 
      $username = Input::get('username');
      $phone = Input::get('phone');
      $address = Input::get('address');
      $isrepeat = DB::table('user_member')->where('phone','=',$phone)->first();
      if($isrepeat){
        return json_encode(array('code'=>400,'msg'=>"对不起,您已经获取过奖品"));
      }else{
        $getinfo = DB::table('user_member')->insert(array('username'=>$username,'phone'=>$phone,'address'=>$address,'time'=>time()));

        return json_encode(array('code'=>200,'msg'=>'提交成功'));
      }


    }




 //    public function action_shareconfig()
	// {
	//   header("Access-Control-Allow-Origin: *"); // 允许任意域名发起的跨域请求  
 //    header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With');  
 //    //$url = "http://donation.youledi.cn/weixinapp/public/index.php/home/index";
 //    //$url = "http://donation.youledi.cn/";
 //    $url = Input::get('url');
 //    $arr = Api::creat_signaturedata($url);
 //    return json_encode(array('arr'=>$arr));
   
	// }

  public function action_shareconfig()
  {
    //$result = Redis::get('youledi_access_token');
      
      $redis = new redis();
      $redis->connect('127.0.0.1', 6380);  
      $redis->auth('pengsu');  //验证密码,通过后才能执行redis操作
      $result = $redis->get('youledi_access_token');
      $result = unserialize($result);
      if($result){
        if($result['expire_time'] > time()){
          $access_token = $result['access_token'];
        }else{
          $access_token = '';
        }
      }else{
        $access_token = '';
      }
      
      if(!$access_token)
      {
        $result = Api::http_curl('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx5cdb35abf2a601fb&secret=b63a32d1930fb91639a568c07a8c6049');
        if(isset($result['errcode'])){
          echo json_encode(array('401',$result));
          exit;
        }
        $access_token = $result['access_token'];
       
          // Save into the cache for 5 minutes
          $data = array(
            'access_token' => $access_token,
            'expire_time' => time()+7000
          );
          $redis->set('youledi_access_token',serialize($data));
      }
      // cache::flush(); 清空所有缓存
       
      $resultone = $redis->get('youledi_jsapi_ticket');
      $resultone = unserialize($resultone);
      if($resultone){ 
        if($resultone['expire_time'] > time()){
          
          $jsapi_ticket = $resultone['jsapi_ticket'];
        }else{
          $jsapi_ticket = '';
        }
    }else{
      $jsapi_ticket = '';
    }

      if(!$jsapi_ticket)
      {
         $res = Api::http_curl('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$access_token.'&type=jsapi');
        if(isset($res['errcode']) AND $res['errcode'] == 0){
          $jsapi_ticket = $res['ticket'];
        }else{
          echo json_encode(array('401',$res));
          exit;
        }
        $data = array(
          'jsapi_ticket' => $jsapi_ticket,
          'expire_time' => time()+7000
        );
          $redis->set('youledi_jsapi_ticket',serialize($data));
      }
      
      
      //if( ! $signature = $this->cache->get('signature')){
      $noncestr = Api::createNonceStr();

        $url = Input::get('url');
        $timestamp = time();
        $signature = 'jsapi_ticket='.$jsapi_ticket.'&noncestr='.$noncestr.'&timestamp='.$timestamp.'&url='.$url;

        $signature = sha1($signature);
        //$this->cache->save('signature',$signature,7100);
      //}
      $arr = array(
            "appId"     => "wx5cdb35abf2a601fb",
            "nonceStr"  => $noncestr,
            "timestamp" => $timestamp,
            "signature" => $signature
        );
      return json_encode(array('arr'=>$arr));

  }















}